﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC01.Models;
using System.Data.Entity;
using System.Collections;

namespace MVC01.Controllers
{
    public class HomeController : Controller
    {
        private PAMVCEntities db = new PAMVCEntities();

        public ActionResult Index()
        {
            ViewBag.Home = "active";

            List<Top3Product> top3Product = new List<Top3Product>();

            foreach (var item in db.Categories.ToList())
            {
                var query = db.ProductCategories.Where(c => c.CategoryId == item.CatId)
                    .OrderBy(p => p.Product.Position)
                    .Select(x => x.Product)
                    .Where(act => act.IsActive == true)
                    .Take(3)
                    .ToList();
                var top3item = new Top3Product();
                top3item.CategoryName = item.CatName;
                top3item.CategoryId = item.CatId;
                if (query != null && query.Count > 0)
                {
                    top3item.Product = query;
                }

                top3Product.Add(top3item);
            }

            return View(top3Product);
        }

        public ActionResult About()
        {
            ViewBag.About = "active";
            return View();
        }

        public ViewResult Product(int cid)
        {
            ViewBag.CatSelected = string.Concat("#cat", cid);
            ViewBag.LoadMoreProduct = cid;
            var productbyCat = db.ProductCategories
                .Where(cat => cat.CategoryId == cid && cat.Product.IsActive == true)
                .OrderByDescending(o => o.Product.DateCreated)
                .OrderBy(pos => pos.Product.Position)
                .Select(p => p.Product).Take(12)
                .ToList();
            return View(productbyCat);
        }

        public ViewResult ProductDetail(int pid, int cid)
        {
            ViewBag.CatSelected = string.Concat("#cat", cid);
            ViewBag.LoadMoreProduct = cid;

            return View(db.Products.Find(pid));
        }

        public ViewResult ProductPromotion()
        {
            ViewBag.ProductPromotion = "active";

            var productbyCat = db.ProductCategories
                .Where(cat => cat.Product.IsActive == true && cat.Product.IsPromotion == true)
                .OrderByDescending(o => o.Product.DateCreated)
                .OrderBy(pos => pos.Product.Position)
                .Select(p => p.Product).Take(12)
                .ToList();
            return View(productbyCat);
        }

        public ViewResult ProductNew()
        {
            ViewBag.ProductNew = "active";

            var productbyCat = db.ProductCategories
                .Where(cat => cat.Product.IsActive == true)
                .OrderByDescending(o => o.Product.DateCreated)
                .OrderBy(pos => pos.Product.Position)
                .Select(p => p.Product).Take(12)
                .ToList();
            return View(productbyCat);
        }

        public ViewResult ProductByCategory(int catid)
        {
            ViewBag.ProductNew = "active";

            var productbyCat = db.ProductCategories
                .Where(cat => cat.Product.IsActive == true)
                .OrderByDescending(o => o.Product.DateCreated)
                .OrderBy(pos => pos.Product.Position)
                .Select(p => p.Product).Take(12)
                .ToList();
            return View(productbyCat);
        }

        public PartialViewResult LoadCategory()
        {
            return PartialView("_Category", db.Categories.ToList());
        }

        public PartialViewResult LoadSlider()
        {
            var slider = db.SliderImages.Where(active => active.Active == true).ToList();
            return PartialView("_Slider", slider);
        }

        public PartialViewResult LoadMoreProduct(int catid)
        {
            List<int?> listProduct = db.ProductCategories.Where(p => p.CategoryId == catid).Select(s => s.ProductId).ToList();

            var productImg = db.ProductImages
                .Where(pi => pi.Position == 1 && pi.Product.IsActive == true && listProduct.Contains(pi.ProductId))                
                .ToList();

            return PartialView("_MoreProduct", productImg);
        }
    }
}
